# forms.py
from django import forms
from .models import PayMain, PayDetail


class PayMainForm(forms.ModelForm):
    class Meta:
        model = PayMain
        fields = ('Eid', 'Pcount', 'Ptotal', 'Pdate', 'other')

    def clean_Pdate(self):
        data = self.cleaned_data['Pdate']
        if len(data) != 8:
            raise forms.ValidationError('采购时间格式不正确，应为8位字符串，例如“20240101”。')
        return data


class PayDetailForm(forms.ModelForm):
    class Meta:
        model = PayDetail
        fields = ('Pid', 'Gid', 'Pcount', 'GPay', 'other')

    def clean_Pcount(self):
        data = self.cleaned_data['Pcount']
        if data <= 0:
            raise forms.ValidationError('采购数量必须大于0')
        return data

    def clean_GPay(self):
        data = self.cleaned_data['GPay']
        if data <= 0:
            raise forms.ValidationError('商品单价必须大于0')
        return data
